from django.db import models
from user.models import User
from user.models import Company, WarZone


# Create your models here.
class Storehouse(models.Model):
    STOREHOUSESTATE = (
        (0, u'已开启'),
        (1, u'已关闭'),
        (2, u'未开启'),
    )
    RUNSTATE = (
        (0, u'在线'),
        (1, u'离线'),
    )
    CONNECTSTATE = (
        (0, u'未连接'),
        (1, u'连接中'),
        (2, u'已连接'),
        (3, u'已拒绝'),
    )
    STORETYPE = (
        (0, u'子仓库'),
        (1, u'主仓库'),
    )
    # user = models.ForeignKey(
    #     User,
    #     verbose_name=u"创建者",
    #     on_delete=models.CASCADE,
    #     related_name='storehouse_user',
    #     null=True,
    # )
    user = models.ManyToManyField(
        User,
        verbose_name=u'所属人员',
        # on_delete=models.CASCADE,
        related_name='storehouse_user',
        null=True
    )
    storehouseName = models.CharField(
        max_length=30,
        verbose_name=u'仓库名称',
        default=u'',
    )
    storehouseState = models.CharField(
        choices=STOREHOUSESTATE,
        max_length=50,
        default=u'0',
        verbose_name=u"仓库状态",
    )
    storeIPType = models.CharField(
        choices=STORETYPE,
        max_length=50,
        default=u'0',
        verbose_name=u"仓库类型",
    )
    connectState = models.CharField(
        choices=CONNECTSTATE,
        max_length=50,
        default=u'0',
        verbose_name=u"仓库连接状态",
        null=True
    )
    runState = models.CharField(
        choices=RUNSTATE,
        max_length=50,
        default=u'0',
        verbose_name=u"运行状态",
    )
    company = models.ForeignKey(
        Company,
        verbose_name=u'所属单位',
        on_delete=models.CASCADE,
        related_name='storehouse_company',
        null=True
    )
    warZone = models.ForeignKey(
        WarZone,
        verbose_name=u'所属战区',
        on_delete=models.CASCADE,
        related_name='storehouse_warzone',
        null=True
    )
    storehouseType = models.CharField(
        max_length=30,
        verbose_name=u'仓库类型',
        default=u'',
    )
    linkName = models.CharField(
        max_length=30,
        verbose_name=u'联系人',
        default=u'',
    )
    linkPhone = models.CharField(
        max_length=30,
        verbose_name=u'联系电话',
        default=u'',
    )
    storehouseDetails = models.CharField(
        max_length=3000,
        verbose_name=u'仓库描述',
        default=u'',
    )
    isMasterStore = models.BooleanField(
        verbose_name=u'是否主仓库',
        default=False
    )
    storeIP = models.GenericIPAddressField(
        verbose_name=u'仓库IP地址',
        default=u'',
        null=True
    )
    storePort = models.CharField(
        verbose_name=u'端口地址',
        default=u'',
        max_length=30,
        null=True
    )
    connectIP = models.GenericIPAddressField(
        verbose_name=u'连接主仓库IP地址',
        default=u'',
        null=True
    )
    connectPort = models.CharField(
        verbose_name=u'连接端口地址',
        default=u'',
        max_length=30,
        null=True
    )
    isRunning = models.BooleanField(
        default=True,
        verbose_name=u'是否运行',
    )
    isOpen = models.BooleanField(
        verbose_name=u'是否开启',
        default=True,
    )
    createTime = models.DateTimeField(
        auto_now_add=True,
        verbose_name=u"创建时间",
    )
    isActive = models.BooleanField(
        verbose_name=u'是否活跃',
        default=True,
    )

    def __unicode__(self):
        return self.storehouseName

    class Meta:
        ordering = ('createTime',)


class StorehouseConnect(models.Model):
    CONNECTSTATE = (
        (0, u'未连接'),
        (1, u'连接中'),
        (2, u'已连接'),
        (3, u'已拒绝'),
    )
    storehouse = models.ForeignKey(
        Storehouse,
        verbose_name=u"仓库连接申请",
        on_delete=models.CASCADE,
        related_name='storehouse_connect',
    )
    connectState = models.CharField(
        choices=CONNECTSTATE,
        max_length=50,
        default=u'1',
        verbose_name=u"仓库连接状态",
    )
    masterStorehouseId = models.CharField(
        max_length=30,
        verbose_name=u'主仓库Id',
        default=u'',
    )
    subStorehouseId = models.CharField(
        max_length=30,
        verbose_name=u'子仓库Id',
        default=u'',
    )
    applyTime = models.DateTimeField(
        auto_now_add=True,
        verbose_name=u"创建时间",
        null=True
    )
    createTime = models.DateTimeField(
        auto_now_add=True,
        verbose_name=u"创建时间",
    )
    isConnect = models.BooleanField(
        verbose_name=u'是否连接',
        default=False,
    )
    isActive = models.BooleanField(
        verbose_name=u'是否活跃',
        default=True,
    )

    def __unicode__(self):
        return self.masterStorehouseId

    class Meta:
        ordering = ('createTime',)
